public class Leet5 {
    public static void main(String[] args) {
        System.out.println(longestPalindrome("xaabacxcabaaxcabaax"));
    }
    static public String longestPalindrome(String s) {
        int max = -100000000,num = 0;
        String re = "";

        for (int i = 0; i < s.length()-1; i++) {
            int l = i-1,r = i+1;
            StringBuilder sb = new StringBuilder(String.valueOf(s.charAt(i)));
            if (i>0&&s.charAt(i-1)==s.charAt(i+1)){
                while (true){
                    if (l==-1||r==s.length()){
                        break;
                    }
                    if (s.charAt(l)==s.charAt(r)){
                        sb.append(s.charAt(r));
                        sb.insert(0,s.charAt(l));
                        l--;
                        r++;
                    }else break;
                }
                if (r-l>max){
                    re = sb.toString();
                    max = r-l-1;
                }
            }

            l = i;r = i+1;
            if (s.length()>1&&s.charAt(l)==s.charAt(r)){
                sb = new StringBuilder();
                while (true){
                    if (l==-1||r==s.length()){
                        break;
                    }
                    if (s.charAt(l)==s.charAt(r)){
                        sb.append(s.charAt(r));
                        sb.insert(0,s.charAt(l));
                        l--;
                        r++;
                    }else break;
                }
                if (r-l>max){
                    re = sb.toString();
                    max = r-l-1;
                }
            }

        }
        if (max ==0||re.length()==0)
            return String.valueOf(s.charAt(0));
        return re;
    }
}
